﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RightControl.Common{
    public class HtmlUtils {

        public static bool ContainChinese(string input) {
            string pattern = "[\u4e00-\u9fbb]";
            return Regex.IsMatch(input, pattern);
        }

        public static string GetMatchedStr(string pattern, string oriStr) {
            Regex reg = new Regex(pattern);
            Match match = reg.Match(oriStr);
            if (match != null && match.Groups.Count > 1) {
                return match.Groups[1].Value;
            }
            return "";
        }

        public static List<string> GetMatchedList(string pattern, string oriStr) {
            List<string> ret = new List<string>();
            Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(oriStr);
            if (mc != null && mc.Count > 0) {
                for (int i = 0; i < mc.Count; i++) {
                    ret.Add(mc[i].Groups[1].Value);
                }              
            }
            return ret;
        }

        /// <summary>  
        /// GMT时间转成本地时间  
        /// </summary>  
        /// <param name="gmt">字符串形式的GMT时间</param>  
        /// <returns></returns>  
        public static DateTime GMT2Local(string gmt) {
            DateTime dt = DateTime.MinValue;
            try {
                string pattern = "";
                if (gmt.IndexOf("+0") != -1) {
                    gmt = gmt.Replace("GMT", "");
                    pattern = "ddd, dd MMM yyyy HH':'mm':'ss zzz";
                }
                if (gmt.ToUpper().IndexOf("GMT") != -1) {
                    pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
                }
                if (pattern != "") {
                    dt = DateTime.ParseExact(gmt, pattern, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal);
                    dt = dt.ToLocalTime();
                } else {
                    dt = Convert.ToDateTime(gmt);
                }
            } catch {
            }
            return dt;
        }

        public static bool IsTelephone(string value) {            
            return Regex.IsMatch(value, @"^(1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)");
        }

        public static bool IsNumber(string strNumber) {
            Regex regex = new Regex("[^0-9]");
            return !regex.IsMatch(strNumber);
        }

        /// <summary>
        /// 英数字组合
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static bool IsAlphabetNumber(string inputStr) {
            Regex regex = new Regex("^[A-Za-z0-9]+$");
            return regex.IsMatch(inputStr);
        }


        /// <summary>  
        /// 时间戳转为C#格式时间  
        /// </summary>  
        /// <param name="timeStamp">Unix时间戳格式</param>  
        /// <returns>C#格式时间</returns>  
        public static DateTime GetTime(string timeStamp) {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time"> DateTime时间格式</param>  
        /// <returns>Unix时间戳格式</returns>  
        public static int ConvertDateTimeInt(System.DateTime time) {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 对列表随机排序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="oriData"></param>
        /// <returns></returns>
        public static List<T> RandomSortList<T>(List<T> oriData) {
            List<T> retList = new List<T>();
            Random rand;

            foreach (var item in oriData) {
                rand = new Random(GetRandomSeed());
                retList.Insert(rand.Next(retList.Count + 1), item);
            }
            return retList;
        }
        
        private static int GetRandomSeed() {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        public static string Escape(string str) {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str) {
                sb.Append((Char.IsLetterOrDigit(c)
                || c == '-' || c == '_' || c == '\\'
                || c == '/' || c == '.') ? c.ToString() : Uri.HexEscape(c));
            }
            return sb.ToString();
        }

        public static string UnEscape(string str) {
            StringBuilder sb = new StringBuilder();
            int len = str.Length;
            int i = 0;
            while (i != len) {
                if (Uri.IsHexEncoding(str, i))
                    sb.Append(Uri.HexUnescape(str, ref i));
                else
                    sb.Append(str[i++]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 随机打乱字段，组合输出
        /// </summary>
        /// <param name="titleLst"></param>
        /// <returns></returns>
        public static String MixFields(List<String> titleLst) {
            titleLst = RandomSortList<String>(titleLst);
            StringBuilder sbTitle = new StringBuilder();
            foreach (var item in titleLst) {
                sbTitle.Append(item + " ");
            }
            return sbTitle.ToString();
        }
    }
}
